package com.aidex.biz.utils;

import cn.hutool.crypto.SecureUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

public class QuXiangHuaAesUtils {
    public static final String QU_XIANG_HUA_KEY = "MCXBIjANke";
    private static final Logger log = LoggerFactory.getLogger(QuXiangHuaAesUtils.class);

    /**
     * 加密
     *
     * @param
     * @param timestamp
     * @return
     */
    public static String encrypt(String timestamp, String dataJson) {
        try {
            String key = String.format("%s%s", QU_XIANG_HUA_KEY, timestamp).substring(0, 16);
            return SecureUtil.aes(key.getBytes(StandardCharsets.UTF_8)).encryptHex(dataJson);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 解密
     *
     * @param timestamp
     * @param dataJson
     * @return
     */
    public static String decrypt(String timestamp, String dataJson) {
        try {
            log.error("趣享花-撞库接口-参数timestamp：" + timestamp);
            log.error("趣享花-撞库接口-参数signs：" + dataJson);
            String key = String.format("%s%s", QU_XIANG_HUA_KEY, timestamp).substring(0, 16);
            log.error("趣享花-撞库接口-key：" + key);
            String decrypt = SecureUtil.aes(key.getBytes(StandardCharsets.UTF_8)).decryptStr(dataJson);
            log.error("趣享花-撞库接口-decrypt：" + decrypt);
            return decrypt;
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public static void main(String[] args) {
        String timestamp = "1752650894693";
//        String dataJson = "{\"mobileMask\":\"177717044\",\"city\":18}";
//        String encrypt = encrypt(timestamp, dataJson);
//        System.out.println(encrypt);
        String decrypt = decrypt(timestamp,
                "49aab7c9118df54524000f136405ab7e1c68e4f5421fb6a2125c48bb416c2b773b63faea93c588a490728ea59fd52c1fda8ad5c4f9d16456adda12d91d6f673e");
        System.out.println(decrypt);
    }
}
